// 用户权限

import { MenuItem } from "ant-design-vue";
import { asyncRoutes, defaultRoutes } from "../../router";

// 是否有权限
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    //是否有这个权限meta中
    return roles.some((role) => route.meta.roles.includes(role));
  } else {
    return false;
  }
}
// 过滤当前角色的路由
export function filterRoutes(routes, old) {
  const res = [];
  routes.forEach((r) => {
    let first = old.filter((a) => a.title == r.name);
    const temp = { ...first[0] };
    // debugger
    if (r.children) {
      r.children = filterRoutes(r.children, first[0].children);
    }

    res.push(temp);
  });

  return res;
}

// 过滤当前角色的路由
export function filterAsyncRoutes(routes) {
  const res = [];

  routes.forEach((route) => {
    const tmp = { ...route, title: route.meta.title };
    // if (hasPermission(roles, tmp)) {
    // 如果有孩子则递归，没孩子跳过下一个路由
    if (tmp.children) {
      tmp.children = filterAsyncRoutes(tmp.children);
    }
    // }
    res.push(tmp);
  });

  return res;
}
// export function filterAsyncRoutes(routes, roles) {
//     const res = [];
//     routes.forEach((route) => {
//       const tmp = { ...route,myname: route.meta.title };

//       if (hasPermission(roles, tmp)) {
//         // 如果有孩子则递归，没孩子跳过下一个路由
//         if (tmp.children) {
//           tmp.children = filterAsyncRoutes(tmp.children, roles);
//         }
//       }
//       res.push(tmp);
//     });

//     return res;
//   }

const state = {
  // 最终的路由表
  routes: localStorage.getItem("myroutes") || [],
  addRoutes: [],
  roles: localStorage.getItem("roles") || false,
};
const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes;
    let r = defaultRoutes.concat(routes);
    localStorage.setItem("myroutes", JSON.stringify(r));
    // state.routes = r;
  },
  setRoles: (state, payload) => {
    state.roles = payload;
    localStorage.setItem("roles", payload);
  },
};
const actions = {
  generateRoutes({ commit, rootState }, roles) {
    return new Promise((resolve) => {
      debugger;
      //   console.log(asyncRoutes, "asyncRoutes--------");
      let lastaccessdRoutes;
      let accessdRoutes;
      // 如果是管理员
      if (roles.includes("admin")) {
        lastaccessdRoutes = asyncRoutes || [];
      } else {
        // 动态权限
        accessdRoutes = filterAsyncRoutes(asyncRoutes, roles);
        console.log(accessdRoutes, "99999999999999");
        // console.log(rootState.routers, "1111111111");
        // let routers = rootState.routers;
        // 接口请求的权限
        let routers = JSON.parse(localStorage.getItem("routers"));
        lastaccessdRoutes = filterRoutes(routers, accessdRoutes);

        console.log(
          lastaccessdRoutes,
          "lastaccessdRouteslastaccessdRouteslastaccessdRoutes"
        );
        // lastaccessdRoutes.unshift(asyncRoutes[0])
        // accessdRoutes = filterAsyncRoutes(asyncRoutes,);
      }
      commit("setRoles", true);
      commit("SET_ROUTES", lastaccessdRoutes);
      resolve(lastaccessdRoutes);
    });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
